#ifndef CLSV_H_
#define CLSV_H_

#define MAX_THREADS 100
#define MAX_NICK 40
#define MAX_LISTS 300
#define MAX_LISTU 512
#define MAX_CHAT 79
#define MAX_MESSAGE 80
#define MAX_USERS 10
#define MAX_SESSIONS 90
#define MAX_OPCODE CHATS
#define _MAX_DATA_ 256
#define MSG_KEY 0xBE0
#define MSG_KEY2 0xBE1
#define MSG_MAX 1024
#define MAX_ERROR 256
#define OK_ 0
#define QPERM 0666

#define NEW_SERVER 3

/* Errors */
#define INCON_OPC -1 //inconsistent operation code
#define NO_SERVER -2 //the session server does not exist
#define ERROR_SERVER -4 // internal error of the server
#define ERROR_LIST   -5 // error using listADT in the server
#define ERROR_SESS_NOT_EXISTS -6
#define ERROR_CONN -7
#define ERROR_NICK_EXISTS -8
#define ERROR_ADDING_USER -9
#define ERROR_LISTS -10
#define ERROR_NO_SESSION -11
#define ERROR_CHAT -12
#define ERROR_NO_USERS -13

#define NUM_THREADS 2

typedef struct
{
	char opcode;
	char msg[_MAX_DATA_];
} packetT;

typedef struct {
	//int upid; 
	int session;
	char nick[MAX_NICK+1];
} CLSV_CONN;

typedef struct {
	//int upid;
} CLSV_LISTS;

typedef struct {
	//int upid;
	int session;
} CLSV_LISTU;

typedef struct {
	//int upid;
	int session;
	char nick[MAX_NICK+1];
} CLSV_DISC;

typedef struct {
	//int upid;
	char nick[MAX_NICK+1];	
} CLSV_NICK;

typedef struct {
	//int upid;
	char nick[MAX_NICK+1];
	char msg[MAX_CHAT+1];
} CLSV_CHAT;

/*
typedef struct {
	int upid;
	int spid;
	int status;
	int session;
} CLSV_INFO;
*/
typedef struct {
	//int spid;
	int status;
} SVCL_CONN;

typedef struct {
	int status;
	//int spid;
	char list[MAX_LISTS+1];
} SVCL_LISTS;

typedef struct {
	int status;
	//int spid;
	char list[MAX_LISTU+1];
} SVCL_LISTU;

typedef struct {
	int status;
} SVCL_NICK;

typedef struct {
	int status;
	char nick[MAX_NICK+1];
	char msg[MAX_CHAT+1];
} SVCL_CHAT;	
/*
typedef struct {
	int status;
} SVCL_INFO;
*/
typedef struct {
	int nomessage;
	char message[MAX_MESSAGE+1];
	char nick[MAX_NICK+1];
} messT;

typedef union
{
	CLSV_CONN		clsv_conn;
	CLSV_LISTS		clsv_lists;
	CLSV_LISTU		clsv_listu;
	CLSV_DISC		clsv_disc;
	CLSV_NICK		clsv_nick;
	CLSV_CHAT		clsv_chat;
	//CLSV_INFO		clsv_info;
	SVCL_CONN		svcl_conn;
	SVCL_LISTS		svcl_lists;
	SVCL_LISTU		svcl_listu;
	SVCL_CHAT		svcl_chat;
	SVCL_NICK		svcl_nick;
	//SVCL_INFO		svcl_info;
} DATA;

typedef enum {CONN = 1, LISTS, LISTU, DISCONNECT, CHATR, CHATS} OPC;

typedef struct
{
	OPC opcode;						
	DATA data;						
} CLSV_BUFFER; 

void myError(int code);
void fatal(char * s);

#endif /*CLSV_H_*/
